/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.server.httpservice;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpResponse;
import org.apache.log4j.Logger;

/**
 *
 * @author zhangyun
 */
public class HttpResponseParser {

//    static Logger logger = Logger.getLogger(HttpResponseParser.class.getName());

    public static String getResponseContent(HttpResponse response) {

        InputStream reponseStream = null;
        try {
            reponseStream = response.getEntity().getContent();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] bytesContent = new byte[1024];
            int bytesReadIn = 0;

            while ((bytesReadIn = reponseStream.read(bytesContent, 0, 1024)) > 0) {
                out.write(bytesContent, 0, bytesReadIn);
            }
            out.close();
            byte[] rep = out.toByteArray();
            return new String(rep, "UTF-8");
        } catch (IOException | UnsupportedOperationException ex) {
//            logger.error(ex.getMessage(), ex);
            return null;
        } finally {
            try {
                reponseStream.close();
            } catch (Exception ex) {
//                logger.error(ex.getMessage(), ex);
            }
        }
    }

}
